package com.woniu.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.dao.YUserAddressMapper;
import com.woniu.model.entity.YUserAddress;
import com.woniu.model.param.AddAddressParam;
import com.woniu.model.param.UpdateAddressParam;
import com.woniu.service.YUserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 胡歌
 * @since 2024年05月22日
 */
@Service
public class YUserAddressServiceImpl extends ServiceImpl<YUserAddressMapper, YUserAddress> implements YUserAddressService {

    @Autowired
    private YUserAddressMapper userAddressMapper;
    @Override
    public void addAddress(AddAddressParam param) throws Exception {
        QueryWrapper<YUserAddress> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",param.getUserId());
        YUserAddress userAddress = new YUserAddress();
        BeanUtil.copyProperties(param, userAddress);
        userAddressMapper.insert(userAddress);
    }

    @Override
    public void updateAddress(UpdateAddressParam param) throws Exception {
        QueryWrapper<YUserAddress> Wrapper = new QueryWrapper<>();
        Wrapper.eq("address_id",param.getAddressId());
        YUserAddress userAddress = userAddressMapper.selectById(param.getAddressId());
        userAddress.setAddressName(param.getAddressName());
        userAddress.setPhoneNum(param.getPhoneNum());
        userAddress.setName(param.getName());
        userAddressMapper.update(userAddress,Wrapper);
    }

    @Override
    public List<YUserAddress> selectAddress(Integer userId) {
        QueryWrapper<YUserAddress> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        return userAddressMapper.selectList(wrapper);
    }
}
